package com.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.demo.model.Student;
import com.demo.service.StudentService;

@Controller
public class StudentController {

	@Autowired
	private StudentService studentService;

//	@RequestMapping("/student/list.htm")
//	public String list(String username,
//			@RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
//			@RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize, Model model) {
//		List<Student> students = studentService.getStudents(username, pageNo, pageSize);
//		model.addAttribute("students", students);
//		model.addAttribute("username", username);
//		model.addAttribute("pageNo", pageNo);
//		int total = studentService.count(username);
//		int totalPageNo = total / pageSize;
//		if (total % pageSize > 0) {
//			totalPageNo++;
//		}
//		model.addAttribute("totalPageNo", totalPageNo);
//		return "student/list";
//	}

	@RequestMapping("/student/input.htm")
	public String input(@RequestParam(value = "id", required = false, defaultValue = "0") int id, Model model) {
		if (id > 0) {
			Student student = studentService.getStudentById(id);
			model.addAttribute("student", student);
		}
		return "student/input";
	}

	@RequestMapping("/student/login_form.htm")
	public String loginForm(@RequestParam(value = "id", required = false, defaultValue = "0") int id, Model model) {
		if (id > 0) {
			Student student = studentService.getStudentById(id);
			model.addAttribute("student", student);
		}
		return "student/login";
	}

	@RequestMapping("/student/login.htm")
	public String login(String username, String password, Model model) {
		boolean result = studentService.login(username, password);
		if (result) {
			return "redirect:/student/list.htm";
		} else {
			model.addAttribute("result", result);
			return "student/login";
		}
	}

	@RequestMapping("/student/update.htm")
	public String update(Student student) {
		studentService.updateStudent(student);
		return "redirect:/student/list.htm";
	}

	@RequestMapping("/student/delete.htm")
	public String delete(int id) {
		studentService.deleteStudent(id);
		return "redirect:/student/list.htm";
	}

	// public void save(Student student) {
	// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd
	// HH:mm:ss");
	// student.setLoginTime(formatter.format(LocalDateTime.now()));
	// studentService.save(student);
	// }
	//
	// public void delete(long id) {
	// studentService.delete(id);
	// }
	//
	// public void update(Student student){
	// studentService.updateById(student);
	// }
	//
	// public void getStudent(long id){
	// studentService.getStudentById(id);
	// }
	//
	// public void getStudentAll(){
	// studentService.getStudent();
	// }

}
